package com.fansl.allround.daemon.quartz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.allround.common.core.util.R;
import com.fansl.allround.daemon.quartz.dto.ScheduleJobDto;
import com.fansl.allround.daemon.quartz.entity.ScheduleJob;
import com.fansl.allround.daemon.quartz.entity.ScheduleJobLog;
import com.fansl.allround.daemon.quartz.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author fansl
 * @Description: TODO
 * @date 2020/1/10 17:52
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务分页查询
     *
     * @param page
     * @param scheduleJob
     * @return
     */
    @GetMapping("/page")
    public R pageList(Page page, ScheduleJob scheduleJob) {
        return new R(scheduleJobService.page(page, Wrappers.query(scheduleJob)));
    }

    /**
     * 查询定时任务信息
     *
     * @param jobId
     * @return
     */
    @GetMapping("/info/{jobId}")
    public R info(@PathVariable Long jobId) {
        return new R(scheduleJobService.getById(jobId));
    }

    /**
     * 保存定时任务
     *
     * @param scheduleJob
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody ScheduleJobDto scheduleJob) {
        return new R(scheduleJobService.saveJob(scheduleJob));
    }

    /**
     * 修改定时任务
     *
     * @param scheduleJob
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody ScheduleJobDto scheduleJob) {
        return new R(scheduleJobService.updateJob(scheduleJob));
    }

    /**
     * 删除定时任务
     *
     * @param jobId
     * @return
     */
    @DeleteMapping("/delete/{jobId}")
    public R delete(@PathVariable Long jobId) {
        return new R(scheduleJobService.deleteJob(jobId));
    }

    @GetMapping("/run/{jobId}")
    public R runJob(@PathVariable Long jobId) {
        return new R(scheduleJobService.runJob(jobId));
    }

    @GetMapping("/pause/{jobId}")
    public R pauseJob(@PathVariable Long jobId) {
        return new R(scheduleJobService.pauseJob(jobId));
    }

    @GetMapping("/resume/{jobId}")
    public R resumeJob(@PathVariable Long jobId) {
        return new R(scheduleJobService.resumeJob(jobId));
    }

    @GetMapping("/log/info/{jobLogId}")
    public R infoLog(@PathVariable Long jobLogId) {
        return new R(scheduleJobService.infoLog(jobLogId));
    }

    @GetMapping("/log/{jobId}")
    public R listJobLog(@PathVariable Long jobId) {
        return new R(scheduleJobService.listJobLog(jobId));
    }

    @GetMapping("/log/page")
    public R pageJobLog(Page page, ScheduleJobLog scheduleJobLog) {
        return new R(scheduleJobService.pageJobLog(page, scheduleJobLog));
    }
}
